<template>
  <div>
    <a-form :form="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 10 }">
      <a-form-item v-show="false">
        <a-input v-decorator="['configMd5']"/>
      </a-form-item>
      <a-form-item label="网站名称">
        <a-input v-decorator="['webName',{rules: [{required: true, message:'请输入名称'}]}]"/>
      </a-form-item>
      <a-form-item label="logo图片">
        <a-input v-decorator="['logoPic',{rules: [{required: true, message:'请输入名称'}]}]"/>
      </a-form-item>
      <a-form-item label="网站域名">
        <a-input v-decorator="['host',{rules: [{required: true, message:'请输入网站域名，用于第三方登录回调'}]}]" placeholder="请输入网站域名，用于第三方登录回调"/>
      </a-form-item>
      <a-form-item label="管理员邮箱账号">
        <a-input v-decorator="['email']" placeholder="发送邮件，未配置将不发送邮件"/>
      </a-form-item>
      <a-form-item label="管理员邮箱名">
        <a-input v-decorator="['emailName']" placeholder="发送邮件，未配置将不发送邮件"/>
      </a-form-item>
      <a-form-item label="管理员邮箱密码">
        <a-input v-decorator="['emailPass']" placeholder="发送邮件，未配置将不发送邮件"/>
      </a-form-item>
      <a-form-item :wrapperCol="{offset:4}">
        <a-button type="primary" @click="handlerOk">保存</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script>
import { saveConfig, queryConfig } from '@/api/manage'

export default {
  name: 'BaseConfig',
  data () {
    return {
      form: this.$form.createForm(this)
    }
  },
  methods: {
    initData () {
      queryConfig({
        configTypeEnum: 'WEB_CONFIG',
        configKeyEnum: 'WEB',
        queryContent: true
      }).then(res => {
        if (res.data && res.data.length === 1) {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              ...JSON.parse(res.data[0].configContent),
              configMd5: res.data[0].configMd5
            })
          })
        }
      })
    },
    handlerOk () {
      this.form.validateFields((errors, values) => {
        if (!errors) {
          var data = {
            configType: 'WEB_CONFIG',
            configKey: 'WEB',
            configName: '网站配置',
            configContent: JSON.stringify({
              webName: values.webName,
              host: values.host,
              logoPic: values.logoPic,
              email: values.email,
              emailName: values.emailName,
              emailPass: values.emailPass
            }),
            configMd5: values.configMd5
          }
          saveConfig(data).then(res => {
            this.$message.info('保存成功')
          })
        }
      })
    }
  },
  activated () {
    this.initData()
  }
}
</script>
